package com.ztom.top100;

/**
 * 旋转图像
 * <p>
 * https://leetcode-cn.com/problems/rotate-image/
 *
 * @author ZhangTao
 */
public class Code22Rotate {

    public void rotate(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;

        int a = 0;
        int b = 0;
        int c = m - 1;
        int d = n - 1;
        while (a < c) {
            rotate(matrix, a++, b++, c--, d--);
        }
    }

    private void rotate(int[][] matrix, int a, int b, int c, int d) {
        for (int i = 0; i < d - b; i++) {
            int tmp = matrix[a][b + i];
            matrix[a][b + i] = matrix[c - i][b];
            matrix[c - i][b] = matrix[c][d - i];
            matrix[c][d - i] = matrix[a + i][d];
            matrix[a + i][d] = tmp;
        }
    }
}
